package com.person.modules.sys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.person.modules.sys.dao.SysCompanyDao;
import com.person.modules.sys.entity.SysAuditEntity;
import com.person.modules.sys.entity.SysCompanyEntity;
import com.person.modules.sys.entity.SysUserEntity;
import com.person.modules.sys.service.SysAuditService;
import com.person.modules.sys.service.SysCompanyService;
import com.person.modules.sys.service.SysUserService;
import com.person.modules.sys.shiro.ShiroUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service("SysCompanyService")
public class SysCompanyServiceImpl extends ServiceImpl<SysCompanyDao, SysCompanyEntity> implements SysCompanyService {
    @Autowired
    private SysCompanyDao sysCompanyDao;
    @Autowired
    private SysAuditService sysAuditService;
    @Autowired
    SysCompanyService sysCompanyService;
    @Autowired
    SysUserService sysUserService;


    @Override
    public SysCompanyEntity queryByCompanyName(String companyName) {
         SysCompanyEntity sysCompanyEntity=sysCompanyDao.queryByCompanyName(companyName);
           return sysCompanyEntity;
    }

    @Override
    public SysCompanyEntity queryByUserName(String userName) {
        return sysCompanyDao.queryByUserName(userName);
    }

    //查询所有未审核公司的信息
    @Override
    public List<SysCompanyEntity> getCompanyList(int status) {
        List <SysCompanyEntity> list=sysCompanyDao.getCompanyList(status);
        return list;
    }

    @Override
    public Integer getCompanyCount() {
        return sysCompanyDao.getCount();
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void register(SysCompanyEntity sysCompanyEntity) {
        sysCompanyEntity.setStatus(0);
        sysCompanyDao.add(sysCompanyEntity);
        Long companyId=sysCompanyEntity.getId();
        //修改公司管理员账号的信息
        SysUserEntity sysUserEntity=new SysUserEntity();
        sysUserEntity.setUsername(sysCompanyEntity.getUsername());
        sysUserEntity.setStatus(0);
        //获取盐
        String salt = RandomStringUtils.randomAlphanumeric(20);
        sysUserEntity.setSalt(salt);
        sysUserEntity.setPassword( ShiroUtils.sha256(sysCompanyEntity.getPassword(), salt));
        sysUserEntity.setDeptId(Long.valueOf(1));
        sysUserEntity.setCompanyId(companyId);
        sysUserEntity.setRoleId(Long.valueOf(2));
        Date date = new Date();
        sysUserEntity.setCreateTime(date);
        sysUserService.save(sysUserEntity);
    }

    @Override
    public void registerPic(String newFileName,String compName) {
        sysCompanyDao.savePicUrl(newFileName,compName);
    }
}
